﻿// This source file is part of WallpaperControl, a tool for automatic changing 
// of the desktop wallpaper.
// 
// Visit http://www.wallpaper-control.xe.cx/ and 
// http://wpcontrol.codeplex.com/ for more.
//
// Copyright (c) 2009 Niklas Kroll
//
// Licensed under the GPLv2.
// See http://www.gnu.org/licenses/gpl-2.0.html for details.
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either version 2
// of the License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.

using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;

namespace wpControl1
{
    /// <summary>
    /// Performs and checks for wallpaper changing.
    /// </summary>
    class ChangeClient
    {
        /// <summary>
        /// Indicates whether WallpaperControl should check if a wallpaper has to be 
        /// changed.
        /// </summary>
        internal bool CheckRequested;

        /// <summary>
        /// The timer that counts down the interval.
        /// </summary>
        internal Timer IntervalTimer;

        /// <summary>
        /// Starts a new wallpaper checking session.
        /// </summary>
        internal void StartSession()
        {
            this.CheckRequested = true;
            Program.WallpaperManager.SetDefaultValues();
            // Set first wallpaper in list
            Program.WallpaperManager.SetWallpaper(0);
            // Start checking and set next wallpaper
            if (Program.ConditionManager.CondType == 1)
            {
                this.CheckTime();
            }
            else if (Program.ConditionManager.CondType == 2)
            {
                this.CheckInterval();
            }
            else if (Program.ConditionManager.CondType == 3)
            {

            }
            else
            {
                MessageBox.Show(Program.LanguageManager.GetString("Error_StartSession_NoCond"), Program.LanguageManager.GetString("Caption_Error"), MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            
        }

        /// <summary>
        /// Stops the running checking session.
        /// </summary>
        internal void StopSession()
        {
            // If interval condition is set stop the timer
            if (Program.ConditionManager.CondType == 2)
            {
                IntervalTimer.Stop();
            }
            // Stops time checking and keeps indication up-to-date
            this.CheckRequested = false;
        }

        /// <summary>
        /// Checks if the interval is over and calls the set next wallpaper event.
        /// </summary>
        private void CheckInterval()
        {
            // Create timer
            IntervalTimer = new Timer();

            if (CheckRequested)
            {
                // Set first wallpaper
                Program.WallpaperManager.SetWallpaper(0);
                // Change wallpaper after selected interval is over
                IntervalTimer.Interval = Program.ConditionManager.ChangeInterval * 1000;
                IntervalTimer.Tick += new EventHandler(IntervalTimer_Event);
                IntervalTimer.Start();
            }
        }

        /// <summary>
        /// Checks if the specified time condition is now and calls the set next 
        /// wallpaper event.
        /// </summary>
        private void CheckTime()
        {
            while (CheckRequested)
            {
                // Check if time is reached
                if (Program.ConditionManager.ChangeTime.Hour == DateTime.Now.Hour && Program.ConditionManager.ChangeTime.Minute == DateTime.Now.Minute && Program.ConditionManager.ChangeTime.Second == DateTime.Now.Second)
                {
                    Program.WallpaperManager.SetNextWallpaper();
                }
                // Sleep for performance saving
                System.Threading.Thread.Sleep(1000);
            }
        }

        private void IntervalTimer_Event(object sender, EventArgs e)
        {
            Program.WallpaperManager.SetNextWallpaper();
        }

        private void CheckStartup()
        {
            if (this.CheckRequested)
            {
                int nextWallpaper = Program.ConditionManager.ChangeIndex + 1;
                Program.WallpaperManager.NextWallpaperIndex = nextWallpaper;
                Program.WallpaperManager.SetNextWallpaper();
            }
        }
    }
}
